Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Text

Friend Class Handler

#Region "Properties"

	Friend _Exception As ExceptionInfo
	Private _DoEventLog As Boolean
	Private _DoFileLog As Boolean

	Friend ReadOnly Property Exception() As ExceptionInfo
		Get
			Return _Exception
		End Get
	End Property

	Friend ReadOnly Property EventLog() As Boolean
		Get
			Return _DoEventLog
		End Get
	End Property

	Friend ReadOnly Property FileLog() As Boolean
		Get
			Return _DoFileLog
		End Get
	End Property

#End Region

#Region "Constructors"

	Friend Sub New(ByVal ex As ExceptionInfo, ByVal blnDoEventLog As Boolean, ByVal blnDoFileLog As Boolean)

		_Exception = ex
		_DoEventLog = blnDoEventLog
		_DoFileLog = blnDoFileLog

		If blnDoEventLog Then
			DoEventLog()
		End If

		If blnDoFileLog Then
			DoFileLog()
		End If

	End Sub

#End Region

#Region "Do actions"

	Private Sub DoEventLog()

		Dim objStringBuilder As New StringBuilder

		With objStringBuilder
			.Append(My.Resources.Messages.Title & Environment.NewLine)
			.Append(My.Resources.Messages.Type & ": " & Exception.BaseException.GetType().FullName & Environment.NewLine)
			.Append(My.Resources.Messages.Message & ": " & Exception.BaseException.Message & Environment.NewLine)
			.Append(My.Resources.Messages.Source & ": " & Exception.BaseException.Source & Environment.NewLine)
			.Append(My.Resources.Messages.Method & ": " & Exception.BaseException.TargetSite.Name & Environment.NewLine)
			.Append(Environment.NewLine)
			.Append(My.Resources.Messages.Details & ": " & Environment.NewLine & Exception.BaseException.StackTrace & Environment.NewLine)
			.Append(Environment.NewLine)
			.Append(Environment.NewLine)
			.Append(My.Resources.Messages.ErrooReferenceNotice)
		End With

		Try
			Diagnostics.EventLog.WriteEntry(Config.AppName, objStringBuilder.ToString(), EventLogEntryType.Error, 0)
		Catch

		End Try

	End Sub

	Private Sub DoFileLog()

		Try

			If Not Directory.Exists(Config.LogPath) Then
				Directory.CreateDirectory(Config.LogPath)
			End If

			Dim strLogPath As String = Config.LogPath + _Exception.ID & ".htm"

			Using objStreamWriter As New StreamWriter(strLogPath, True, Config.Encoding)
				objStreamWriter.Write(_Exception.HTML)
				objStreamWriter.Dispose()
			End Using

		Catch

		End Try

	End Sub

#End Region

End Class